package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class HomeActivity extends AppCompatActivity {
    private static final String TAG = "HomeActivity";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_EMAIL = "email";
    private static final String KEY_INTEREST = "interest";
    private static final String KEY_CURRENT_FRAGMENT = "current_fragment";
    
    private String username;
    private String email;
    private String interest = "未选择";
    private Button btnNavHome;
    private Button btnNavProfile;
    
    private HomeFragment homeFragment;
    private ProfileFragment profileFragment;
    private String currentFragmentTag = "home";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_home);

        // 接收传递过来的用户名和邮箱
        if (savedInstanceState != null) {
            // 恢复保存的状态
            username = savedInstanceState.getString(KEY_USERNAME);
            email = savedInstanceState.getString(KEY_EMAIL);
            interest = savedInstanceState.getString(KEY_INTEREST, "未选择");
            currentFragmentTag = savedInstanceState.getString(KEY_CURRENT_FRAGMENT, "home");
            
            // 恢复 Fragment
            FragmentManager fm = getSupportFragmentManager();
            homeFragment = (HomeFragment) fm.findFragmentByTag("home");
            profileFragment = (ProfileFragment) fm.findFragmentByTag("profile");
        } else {
            username = getIntent().getStringExtra("username");
            email = getIntent().getStringExtra("email");
            if (username == null) {
                username = "";
            }
            if (email == null) {
                email = username; // 兼容旧版本，如果没有email就使用username
            }
        }

        // 初始化控件
        btnNavHome = findViewById(R.id.btn_nav_home);
        btnNavProfile = findViewById(R.id.btn_nav_profile);

        // 如果是初次创建，显示首页 Fragment
        if (savedInstanceState == null) {
            showHomeFragment();
        } else {
            // 恢复上次显示的 Fragment
            if ("home".equals(currentFragmentTag)) {
                updateNavigationState(true);
            } else {
                updateNavigationState(false);
            }
        }

        // 底部导航按钮点击事件
        btnNavHome.setOnClickListener(v -> showHomeFragment());
        btnNavProfile.setOnClickListener(v -> showProfileFragment());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_USERNAME, username);
        outState.putString(KEY_EMAIL, email);
        outState.putString(KEY_INTEREST, interest);
        outState.putString(KEY_CURRENT_FRAGMENT, currentFragmentTag);
    }

    /**
     * 显示首页 Fragment
     */
    private void showHomeFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        // 隐藏所有 Fragment
        if (profileFragment != null) {
            transaction.hide(profileFragment);
        }

        // 显示或创建首页 Fragment
        if (homeFragment == null) {
            homeFragment = HomeFragment.newInstance(interest);
            transaction.add(R.id.fragment_container, homeFragment, "home");
        } else {
            transaction.show(homeFragment);
        }

        transaction.commit();
        currentFragmentTag = "home";
        updateNavigationState(true);
    }

    /**
     * 显示我的 Fragment
     */
    private void showProfileFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();

        // 隐藏所有 Fragment
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }

        // 显示或创建我的 Fragment
        if (profileFragment == null) {
            profileFragment = ProfileFragment.newInstance(username, email, interest);
            transaction.add(R.id.fragment_container, profileFragment, "profile");
        } else {
            transaction.show(profileFragment);
        }

        transaction.commit();
        currentFragmentTag = "profile";
        updateNavigationState(false);
    }

    /**
     * 更新导航按钮状态
     */
    private void updateNavigationState(boolean isHomeSelected) {
        if (isHomeSelected) {
            btnNavHome.setEnabled(false);
            btnNavProfile.setEnabled(true);
        } else {
            btnNavHome.setEnabled(true);
            btnNavProfile.setEnabled(false);
        }
    }

    /**
     * 兴趣更新时的回调
     */
    public void onInterestUpdated(String newInterest) {
        this.interest = newInterest;
        
        // 同步更新首页 Fragment
        if (homeFragment != null) {
            homeFragment.updateInterest(newInterest);
        }
        
        // 同步更新我的 Fragment
        if (profileFragment != null) {
            profileFragment.updateInterest(newInterest);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "onNewIntent");
    }
}
